package service

import (
	"context"
	"qld2devservice/proto"
	"qld2devservice/utils"

	"google.golang.org/grpc"
)

const (
	ADDRESS_CARD = "qld2cardservice:8458"
)

func CheckCardInfo(boxOutletSn, cardNum string, devType int64) (retCode int64, bRet bool, errMsg string) {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial(ADDRESS_CARD, grpc.WithInsecure())
	if err != nil {
		errNew := utils.WrapError(err, "CheckCardInfo,gRPC服务器连接失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return 301, false, errNew.Error() + errNew.Msg()
	}
	defer conn.Close()

	// 初始化 gRPC 客户端
	client := proto.NewCardInfoServiceClient(conn)
	res := proto.CheckCardInfoRequest{
		DevSnNum: boxOutletSn,
		CardNum:  cardNum,
		DevType:  devType,
	}
	utils.MyPrint(utils.PRINT_INFO, "CheckCardInfo request", res)
	resp, err := client.CheckCardInfo(context.Background(), &res)
	if err != nil {
		errNew := utils.WrapError(err, "CheckCardInfo的gRPC调用失败")
		utils.MyPrint(utils.PRINT_ERROR, errNew.Msg())
		return 302, false, errNew.Error() + errNew.Msg()
	}
	utils.MyPrint(utils.PRINT_INFO, "CheckCardInfo response", utils.TransProtoToJson(resp))
	utils.MyPrint(utils.PRINT_INFO, "CheckCardInfo msg", resp.ErrMsg)
	return int64(resp.RetCode), resp.BRet, resp.ErrMsg
}
